📦 Min Heap Warehouse Inventory System

Manage parcel weights efficiently using Min Heap — visualize sorting and calculate averages.

📦 Warehouse Parcel Management

🎯 The Challenge:

A supervisor needs to efficiently manage and prioritize parcels by weight using a Min Heap data structure.

📋 Tasks:

  • Accept n parcels with positive integer weights
  • Store valid weights in a Min Heap (smallest at root)
  • Display the Min Heap contents
  • Calculate and show average weight (2 decimal places)
  • Handle invalid inputs gracefully

Problem Specifications

  • Input: n (1 ≤ n ≤ 10), followed by n space-separated weights (1 ≤ weight ≤ 50)
  • Output: Min Heap array representation, then average weight with 2 decimals
  • Edge Case: If no valid weights, output "No valid weight"

Example: n = 5, weights = [3, 9, 2, 6, 8]

Min Heap: 2 6 3 9 8
Average: 5.60
Sum = 28, Count = 5, Average = 28/5 = 5.60

🔄 Min Heap Strategy

Key Concept

  1. Start with an empty Min Heap array
  2. For each parcel weight, if positive, add to heap and maintain heap property
  3. Min Heap property: parent ≤ children (smallest element at root)
  4. Track total sum for average calculation
  5. Display heap contents and calculate average

Note: JavaScript arrays can be used as heaps with proper insertion/heapify logic.

Min Heap Insertion

Time: O(log n) per insertion
Total: O(n log n) for n elements

Array Storage

Space: O(n)
Efficient priority queue operations

🔍 Step-by-Step Min Heap Demo

Ready. Use controls below to start demo.

Min Heap State

Statistics

Total Sum: 0
Count: 0
Average: -
Click Start to run demo

Progress Tracker

1. Parse input weights
2. Validate weight (positive integer)
3. Insert into Min Heap
4. Update sum and count
5. Heapify to maintain property
6. Calculate and display average

🎮 Build Your Warehouse System

Enter data and press Process Parcels

Test Cases

Sample Input
Input: 5
3 9 2 6 8
Expected: 2 6 3 9 8 | Average: 5.60
Invalid Weights
Input: 3
0 -5 10
Expected: Only valid positive weights processed

📊 Analysis & Optimization

Time

O(n log n)

Building heap with n insertions

Space

O(n)

Storing n parcel weights

Why Min Heap?

  • Priority Access: Lightest parcels always at root for quick retrieval
  • Efficient Insertion: O(log n) time to add new parcels
  • Sorted Display: Heap structure maintains partial ordering
  • Real-world: Perfect for priority queues in logistics

Implementation Tips

  • Use array representation: parent at i, children at 2i+1 and 2i+2
  • Heapify up after insertion to maintain min heap property
  • Validate inputs before processing
  • Handle edge cases (no valid weights) appropriately